package com.zf.mapstruct.controller;

import com.zf.mapstruct.converter.MultiObjConverter;
import com.zf.mapstruct.converter.StudentInfoConverter;
import com.zf.mapstruct.converter.UserInfoConverter;
import com.zf.mapstruct.entity.CustomerInfo;
import com.zf.mapstruct.entity.StudentInfo;
import com.zf.mapstruct.entity.UserInfo;
import com.zf.mapstruct.vo.MultiEntityVO;
import com.zf.mapstruct.vo.StudentInfoVO;
import com.zf.mapstruct.vo.UserInfoVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试
 */
@RestController
@RequestMapping("/mapstruct")
public class MapStructController {
    @Resource
    private UserInfoConverter userInfoConverter;
    @Resource
    private StudentInfoConverter studentInfoConverter;
    @Resource
    private MultiObjConverter multiObjConverter;

    @GetMapping("/toUserInfoVO")
    UserInfoVO getUserInfoVo() {
        UserInfo userInfo = getUserInfo(1L, "admin", "超级管理员", "admin@xx.xx", 16);
        // 调用映射器
        return userInfoConverter.toCustomVo(userInfo);
        // return userInfoConverter.toDefaultUserInfoVO(userInfo);
    }


    @GetMapping("/toUserInfoVOList")
    List<UserInfoVO> getUserInfoVoList() {
        ArrayList<UserInfo> list = new ArrayList<>();
        for (long i = 0; i < 3; i++) {
            UserInfo userInfo = getUserInfo(i, "admin", "超级管理员", "admin@xx.xx", (int) (16 + i));
            list.add(userInfo);
        }
        // 调用映射器
        return userInfoConverter.toCustomVoList(list);
    }

    @GetMapping("/toStudentInfoVO")
    StudentInfoVO getStudentInfoVo() {
        // StudentInfo studentInfo = getStudentInfo("张三",14, 3, "male", "class3", LocalDate.now().minusYears(40L), "1980-12-12");
        StudentInfo studentInfo = getStudentInfo("张三", null, 3, "male", "class3", LocalDate.now().minusYears(40L), "1980-12-12");
        // 调用映射器
        return studentInfoConverter.toCustomVo(studentInfo);
    }

    @GetMapping("/toMultiVO")
    MultiEntityVO getMultiVo() {
        UserInfo userInfo = getUserInfo(1L, "admin", "超级管理员", "admin@xx.xx", 16);
        StudentInfo studentInfo = getStudentInfo("张三", 17, 3, "male", "class3", LocalDate.now().minusYears(40L), "1980-12-12");
        CustomerInfo customerInfo = getCustomerInfo("客户王", "德州", "特殊");
        // 调用映射器
        return multiObjConverter.toMulti(userInfo, studentInfo, 20L, customerInfo);
    }


    /**
     * 获取用户信息
     */
    UserInfo getUserInfo(Long userId, String username, String realName, String email, Integer age) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setUsername(username);
        userInfo.setRealName(realName);
        userInfo.setEmail(email);
        userInfo.setAge(age);
        return userInfo;
    }

    /**
     * 获取学生信息
     */
    StudentInfo getStudentInfo(String name, Integer age, Integer studyAge, String gender, String className, LocalDate fatherBirthday, String motherBirthday) {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setName(name);
        studentInfo.setAge(age);
        studentInfo.setStudyAge(studyAge);
        studentInfo.setGender(gender);
        studentInfo.setClassName(className);
        studentInfo.setFatherBirthday(fatherBirthday);
        studentInfo.setMotherBirthday(motherBirthday);
        return studentInfo;
    }

    /**
     * 获取学生信息
     */
    CustomerInfo getCustomerInfo(String customerName, String customerAddr, String gender) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustomerName(customerName);
        customerInfo.setCustomerAddr(customerAddr);
        customerInfo.setGender(gender);
        return customerInfo;
    }
}
